home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / profiler.c < prev    next >
C/C++ Source or Header  |  2000-05-13  |  3KB  |  179 lines

  1. #include "driver.h"
  2. #include "osinline.h"
  3.  
  4. static int use_profiler;
  5.  
  6.  
  7. #define MEMORY 6
  8.  
  9. struct profile_data
  10. {
  11.     unsigned int count[MEMORY][PROFILER_TOTAL];
  12.     unsigned int cpu_context_switches[MEMORY];
  13. };
  14.  
  15. static struct profile_data profile;
  16. static int memory;
  17.  
  18.  
  19. static int FILO_type[10];
  20. static unsigned int FILO_start[10];
  21. static int FILO_length;
  22.  
  23. void profiler_start(void)
  24. {
  25.     use_profiler = 1;
  26.     FILO_length = 0;
  27. }
  28.  
  29. void profiler_stop(void)
  30. {
  31.     use_profiler = 0;
  32. }
  33.  
  34. void profiler_mark(int type)
  35. {
  36.     unsigned int curr_cycles;
  37.  
  38.  
  39.     if (!use_profiler)
  40.     {
  41.         FILO_length = 0;
  42.         return;
  43.     }
  44.  
  45.     if (type >= PROFILER_CPU1 && type <= PROFILER_CPU8)
  46.         profile.cpu_context_switches[memory]++;
  47.  
  48.     curr_cycles = osd_cycles();
  49.  
  50.     if (type != PROFILER_END)
  51.     {
  52.         if (FILO_length >= 10)
  53.         {
  54. logerror("Profiler error: FILO buffer overflow\n");
  55.             return;
  56.         }
  57.  
  58.         if (FILO_length > 0)
  59.         {
  60.             /* handle nested calls */
  61.             profile.count[memory][FILO_type[FILO_length-1]] += (unsigned int)(curr_cycles - FILO_start[FILO_length-1]);
  62.         }
  63.         FILO_type[FILO_length] = type;
  64.         FILO_start[FILO_length] = curr_cycles;
  65.         FILO_length++;
  66.     }
  67.     else
  68.     {
  69.         if (FILO_length <= 0)
  70.         {
  71. logerror("Profiler error: FILO buffer underflow\n");
  72.             return;
  73.         }
  74.  
  75.         profile.count[memory][FILO_type[FILO_length-1]] += (unsigned int)(curr_cycles - FILO_start[FILO_length-1]);
  76.         FILO_length--;
  77.         if (FILO_length > 0)
  78.         {
  79.             /* handle nested calls */
  80.             FILO_start[FILO_length-1] = curr_cycles;
  81.         }
  82.     }
  83. }
  84.  
  85. void profiler_show(struct osd_bitmap *bitmap)
  86. {
  87.     int i,j;
  88.     unsigned int total,normalize;
  89.     unsigned int computed;
  90.     int line;
  91.     char buf[30];
  92.     static char *names[PROFILER_TOTAL] =
  93.     {
  94.         "CPU 1",
  95.         "CPU 2",
  96.         "CPU 3",
  97.         "CPU 4",
  98.         "CPU 5",
  99.         "CPU 6",
  100.         "CPU 7",
  101.         "CPU 8",
  102.         "Video",
  103.         "Blit ",
  104.         "Sound",
  105.         "Mixer",
  106.         "Cllbk",
  107.         "Hiscr",
  108.         "Input",
  109.         "Extra",
  110.         "User1",
  111.         "User2",
  112.         "User3",
  113.         "User4",
  114.         "Prflr",
  115.         "Idle ",
  116.     };
  117.  
  118.  
  119.     if (!use_profiler) return;
  120.  
  121.     profiler_mark(PROFILER_PROFILER);
  122.  
  123.     computed = 0;
  124.     i = 0;
  125.     while (i < PROFILER_PROFILER)
  126.     {
  127.         for (j = 0;j < MEMORY;j++)
  128.             computed += profile.count[j][i];
  129.         i++;
  130.     }
  131.     normalize = computed;
  132.     while (i < PROFILER_TOTAL)
  133.     {
  134.         for (j = 0;j < MEMORY;j++)
  135.             computed += profile.count[j][i];
  136.         i++;
  137.     }
  138.     total = computed;
  139.  
  140.     if (total == 0 || normalize == 0) return;    /* we have been just reset */
  141.  
  142.     line = 0;
  143.     for (i = 0;i < PROFILER_TOTAL;i++)
  144.     {
  145.         computed = 0;
  146.         {
  147.             for (j = 0;j < MEMORY;j++)
  148.                 computed += profile.count[j][i];
  149.         }
  150.         if (computed)
  151.         {
  152.             if (i < PROFILER_PROFILER)
  153.                 sprintf(buf,"%s%3d%%%3d%%",names[i],
  154.                         (computed * 100 + total/2) / total,
  155.                         (computed * 100 + normalize/2) / normalize);
  156.             else
  157.                 sprintf(buf,"%s%3d%%",names[i],
  158.                         (computed * 100 + total/2) / total);
  159.             ui_text(bitmap,buf,0,(line++)*Machine->uifontheight);
  160.         }
  161.     }
  162.  
  163.     computed = 0;
  164.     {
  165.         for (j = 0;j < MEMORY;j++)
  166.             computed += profile.cpu_context_switches[j];
  167.     }
  168.     sprintf(buf,"CPU switches%4d",computed / MEMORY);
  169.     ui_text(bitmap,buf,0,(line++)*Machine->uifontheight);
  170.  
  171.     /* reset the counters */
  172.     memory = (memory + 1) % MEMORY;
  173.     profile.cpu_context_switches[memory] = 0;
  174.     for (i = 0;i < PROFILER_TOTAL;i++)
  175.         profile.count[memory][i] = 0;
  176.  
  177.     profiler_mark(PROFILER_END);
  178. }
  179.